package com.example.day45.surface;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.day45.R;

/**
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @Package com.example.day45.surface
 * @Description:
 * @date 15/10/29 上午10:45
 *
 * 第一步： 继承SurfaceView并实现SurfaceHolder.Callback接口
 * 第二步： SurfaceView.getHolder()获得SurfaceHolder对象
 * 第三步：SurfaceHolder.addCallback(callback)添加回调函数
 * 第四步：SurfaceHolder.lockCanvas()获得Canvas对象并锁定画布
 * 第五步：Canvas绘画
 * 第六步：SurfaceHolder.unlockCanvasAndPost(Canvas canvas)结束锁定画图，并提交改变，将图形显示。
 */


public class PlaySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private boolean isPlay = true;
    private Bitmap[] bitmaps = new Bitmap[4];

    public PlaySurfaceView(Context context) {
        super(context);
        init();
    }

    public PlaySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlaySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public PlaySurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        SurfaceHolder holder = this.getHolder();
        holder.addCallback(this);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        bitmaps[0] = BitmapFactory.decodeResource(getResources(), R.mipmap.a1, options);
        bitmaps[1] = BitmapFactory.decodeResource(getResources(), R.mipmap.a2, options);
        bitmaps[2] = BitmapFactory.decodeResource(getResources(), R.mipmap.a3, options);
        bitmaps[3] = BitmapFactory.decodeResource(getResources(), R.mipmap.a4, options);
    }

    private class PlayTask extends Thread {
        private SurfaceHolder holder;

        public PlayTask(SurfaceHolder holder) {
            this.holder = holder;
        }

        @Override
        public void run() {
            int index = 0;
            Paint paint = new Paint();
            RectF rectF = new RectF(0, 0, 1280, 1920);
            while (isPlay) {
                Canvas canvas = holder.lockCanvas();
                canvas.drawBitmap(bitmaps[index % bitmaps.length], null, rectF, paint);
                index++;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                holder.unlockCanvasAndPost(canvas);
            }

        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new PlayTask(holder).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isPlay = false;
    }


}
